package cn.wolfcode.wms.web.controller;


import cn.wolfcode.wms.domain.Permission;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.service.IPermissionService;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.service.ISystemMenuService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.NeedPermission;
import cn.wolfcode.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.List;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @Autowired//因为需要查询出所有的菜单
    private ISystemMenuService systemMenuService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        PageResult pageResult = roleService.queryResult(qo);
        model.addAttribute("result",pageResult);

        return "role/list";
    }

    @RequestMapping("input")
    public String input(Model model,Long id){

        if (id != null) {
            model.addAttribute("entity",roleService.get(id));
        }
        //共享所有的角色
        List<Permission> list = permissionService.selectList();
        model.addAttribute("pers",permissionService.selectList());
        //共享所有的菜单
        List<SystemMenu> systemMenus = systemMenuService.selectList();
        model.addAttribute("menus",systemMenus);

        return "role/input";
    }

    @RequestMapping("delete")
    public String delete(Long id){

        roleService.delete(id);

        return "redirect:/role/list.do";
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Role d,Long[] perId,Long[] menuIds){

       roleService.saveOrUpdate(d,perId,menuIds);

        return new JSONResult();//象征想返回
    }
}
